package com.wn.class30.tools;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;

import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

public class ODDGenerator {
    private static final FastDateFormat pattern = FastDateFormat.getInstance("yyyyMMddHHmmss");
    private static final AtomicInteger atomicInteger = new AtomicInteger(1);
    private static ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<StringBuilder>();

    /**
     * 【长码生成策略】
     *
     * @时间20180511231532
     * @二位随机数
     * @lock的hash-code编码
     * @param lock 生成的UUID32位参数
     * @return 长码机制
     */
    public static String getC(String lock) {
        StringBuilder builder = new StringBuilder(pattern.format(Instant.now().toEpochMilli()));// 取系统当前时间作为订单号前半部分
        builder.append(Math.abs(lock.hashCode()));// HASH-CODE
        builder.append(atomicInteger.getAndIncrement());// 自增顺序
        threadLocal.set(builder);
        return threadLocal.get().toString();
    }

    /**
     * 【短码生成策略】
     *
     *
     *
     */
    public static String getD() {
        String str = UUID.randomUUID().toString();
        String lock = StringUtils.replace(str, "-", "");
        StringBuilder builder = new StringBuilder(ThreadLocalRandom.current().nextInt(0,999));// 随机数
        builder.append(Math.abs(lock.hashCode()));// HASH-CODE
        builder.append(atomicInteger.getAndIncrement());// 自增顺序
        threadLocal.set(builder);
        return threadLocal.get().toString();
    }

//    public static void main(String[] args) {
//        String str = UUID.randomUUID().toString();
//        System.out.println(str);
//        String str2 = StringUtils.replace(str, "-", "");
//        System.out.println(str2);
//        System.out.println(getC(str2));
//        System.out.println(getD(str2));
//    }
}
